package com.stylefeng.guns.modular.wz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.persistence.dao.WzCouponMapper;
import com.stylefeng.guns.common.persistence.dao.WzUserInfoMapper;
import com.stylefeng.guns.common.persistence.dao.WzUserMapper;
import com.stylefeng.guns.common.persistence.model.WzCoupon;
import com.stylefeng.guns.common.persistence.model.WzUser;
import com.stylefeng.guns.common.persistence.model.WzUserInfo;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.wz.dao.WzAdvSearchDao;
import com.stylefeng.guns.modular.wz.warpper.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: guns
 * @description: 添加广告搜索列
 * @author: Chenchao
 * @create: 2018-04-03 13:37
 **/
@Controller
@RequestMapping("/wzAdvSearch")
public class WzAdvSearchController extends BaseController {
    @Autowired
    private WzAdvSearchDao wzAdvSearchDao;
    @Autowired
    private WzUserMapper userMapper;
    @Autowired
    private WzUserInfoMapper userInfoMapper;
    @Autowired
    private WzCouponMapper couponMapper;

    @RequestMapping(value = "/list_picFile")
    @ResponseBody
    public Object list_picFile(@RequestParam(required = false) String condition_pic) {
        List<Map<String, Object>> wzPicFileList = wzAdvSearchDao.selectPicFiles(null, condition_pic);
        return new PfFileWarpper(wzPicFileList).warp();
    }

    @RequestMapping(value = "/list_coupon")
    @ResponseBody
    public Object list_coupon(@RequestParam(required = false)String condition_coupon,String couponIds,Integer resetting) {
        List<Map<String, Object>> wzCouponList = new ArrayList<>();
        if(StringUtils.isNotBlank(couponIds) && !couponIds.equals("undefined") && resetting == null){
            String[] couponId = couponIds.split(",");
            for(String id:couponId){
                List<Map<String, Object>> wzCoupon=couponMapper.selectMaps(new EntityWrapper<WzCoupon>().eq("coupon_id",id));
                wzCouponList.add(wzCoupon.get(0));
            }
        }else{
            wzCouponList=couponMapper.selectMaps(new EntityWrapper<WzCoupon>().eq("status",1));
        }
        return new WzCouponWarpper(wzCouponList).warp();
    }

    @RequestMapping(value = "/list_questionnaire")
    @ResponseBody
    public Object list_questionnaire() {
        List<Map<String, Object>> wzQuestionnaireList = wzAdvSearchDao.selectBusinessQuestionnaires(null);
        return new WzQuestionnaireWarpper(wzQuestionnaireList).warp();
    }

    /**
     * 获取问题列表
     */
    @RequestMapping(value = "/list_question")
    @ResponseBody
    public Object list_question(@RequestParam(required = false) String condition_question) {
        List<Map<String, Object>> wzQuestionList = wzAdvSearchDao.selectAdvQuestions(null, condition_question);
        return new WzQuestionnaireWarpper(wzQuestionList).warp();
    }

    /**
     * 获取用户列表
     */
    @RequestMapping(value = "/list_user")
    @ResponseBody
    public Object list_user(@RequestParam(required = false) String condition_question,String userCode,Integer resetting) {
        EntityWrapper<WzUser> entityWrapper = new EntityWrapper<WzUser>();
        entityWrapper.eq("role",3);
        if(StringUtils.isNotBlank(userCode) && !userCode.equals("undefined") && resetting == null){
            entityWrapper.eq("user_code",userCode);
        }
        List<Map<String, Object>> users = userMapper.selectMaps(entityWrapper);
        return new WzUserWarpper(users).warp();
    }

    /**
     * 获取用户信息列表
     */
    @RequestMapping(value = "/list_userInfo")
    @ResponseBody
    public Object list_userInfo(@RequestParam(required = false) String condition_question,String userCode) {
        List<Map<String, Object>> users = userInfoMapper.selectMaps(new EntityWrapper<WzUserInfo>().eq("user_code",userCode).eq("type",2));
        return users;
    }

    /**
     * 获取商户信息列表
     */
    @RequestMapping(value = "/list_merchantInfo")
    @ResponseBody
    public Object list_merchantInfo(String condition_userCode) {
        List<Map<String, Object>> merchants = wzAdvSearchDao.selectMerchantInfos(null, condition_userCode);
        return merchants;
    }
}
